#include "roles.h"


namespace BitWin
{


//---- role_t class

role_t::role_t():
    _value(Qt::UserRole)
{}

role_t::role_t(value_t value_):
    _value(value_)
{}

role_t::value_t role_t::value() const
{
    return _value;
}

role_t::operator int() const
{
    return value();
}


//---- RolesSet class

int RolesSet::valueBitPos(int value)
{
    return value - Qt::UserRole;
}

int RolesSet::roleBitPos(role_t role)
{
    return valueBitPos(role.value());
}

RolesSet::RolesSet():
    _data(DATA_INITIALIZER)
{}

RolesSet::RolesSet(role_t role):
    _data(DATA_INITIALIZER)
{
    *this | role;
}

RolesSet& RolesSet::operator|(role_t role)
{
    int pos = roleBitPos(role);
    if ((pos >= 0) && (pos < _data.size()))
        _data.setBit(pos);
    return *this;
}

bool RolesSet::operator&(role_t role)
{
    int pos = roleBitPos(role);
    return (pos >= 0) && (pos < _data.size()) && _data.at(pos);
}

const QBitArray RolesSet::DATA_INITIALIZER(
    valueBitPos(role_t::NumRoles), false
);


//---- miscellaneous

RolesSet operator|(role_t rhs, role_t lhs)
{
    return RolesSet() | rhs | lhs;
}


const role_t
    BitwiseDataRole(role_t::BitwiseDataRole),
    HexDataRole(role_t::HexDataRole),
    DecDataRole(role_t::DecDataRole)
;


} // BitWin namespace end
